Morse Translator

ASCII table (American Standard Code for Information Interchange)

ASCII converts the Char characters to decimal values

Char | ASCII a-> z | 97 -> 122 A-> Z| 65 -> 90 0-> 9 | 48 -> 57 space | 32 null character | \0


// Morse code for blinking a LED
int ledPin = 13;
int dotDelay = 200;

char* letters[] = { // It means an array with the name letters. Char is a type of variable meaning variable , * is a pointer, it points to the characters of the array in order 
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",    // A-I
  ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",  // J-R
  "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."          // S-Z
};
 // Is a second array for numbers 0 to 9, these two arrays are arrays, also known as an array of string literals.
// Example of string literal : ...--- (another could be a az8mhello because a string is several characters together like a word. String literal can be named string.
// We could think as an array of an array of characters
char* numbers[] = { 
  "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
// If i write lettes[0] it means .- because is the first element of the array letters, and letters[26] is --.. 
// Afterwords .- will mean  A and --.. will mean Z
void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  char ch;
  if (Serial.available() > 0){ 
    ch = Serial.read();
    if (ch >= 'a' && ch <= 'z')
    {
      flashSequence(letters[ch - 'a']);
    }
    else if (ch >= 'A' && ch <= 'Z')
    {
      flashSequence(letters[ch - 'A']);
    }
    else if (ch >= '0' && ch <= '9')
    {
      flashSequence(numbers[ch - '0']);
    }
    else if (ch == ' ')
    {
      delay(dotDelay * 4);  // gap between words  
    }
  }
}

void flashSequence(char* sequence){
  int i = 0;
  while (sequence[i] != NULL)
  {
    flashDotOrDash(sequence[i]);
    i++;
  }
  delay(dotDelay * 3);    // gap between letters
}

void flashDotOrDash(char dotOrDash){
  digitalWrite(ledPin, HIGH);
  if (dotOrDash == '.')
  {
    delay(dotDelay);           
  }
  else // must be a dash
  {
    delay(dotDelay * 3);           
  }
  digitalWrite(ledPin, LOW);    
  delay(dotDelay); // gap between flashes
}